﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cofe.Core.Utils;

namespace Cofe.Core.Property
{
    [Export(typeof(ICofeService))]
    [ServicePriority(ServicePriorityAttribute.DefaultPriority_COFE)]
    public class ListAllSupported : IPropertyListSupportedBehavior
    {
        #region Constructor

        public ListAllSupported()
        {
        }

        #endregion

        #region Methods

        public IEnumerable<object> GetSupportedProperty(IPropertyProvider[] allPropertyProviders,
            PropertyRetrivalOptions options = PropertyRetrivalOptions.Default)
        {
            var propWithdefValues = PropertyDictionary
                  .GetPropertiesStringWithDefaultValue()
                  .Select(s => PropertyDictionary.GetProperty(s));

            var _supportedProperty = allPropertyProviders.SelectMany(pp => pp.SupportedDefinitions.GetSupportedProperties())
                //.Union(propWithdefValues)
                .Distinct();

            if (options == PropertyRetrivalOptions.Default)
                return _supportedProperty;

            return _supportedProperty.Where(p => PropertyUtils.GetPropertyRetrivalOptions(p).HasFlag(options));

        }

        #endregion

        #region Data

        private IPropertyDictionary _propertyDictionary = null;

        #endregion
        #region Public Properties

        public IPropertyDictionary PropertyDictionary
        {
            get { return _propertyDictionary ?? CofeServices.ServiceLocater.FindService<IPropertyDictionary>(); }
            set { _propertyDictionary = value; }
        }


        public PropertyRetrivalOptions SupportOption
        {
            get { return PropertyRetrivalOptions.Default | PropertyRetrivalOptions.First | PropertyRetrivalOptions.HandleOnes | PropertyRetrivalOptions.Aggregate; }
        }

        #endregion


    }
}
